<?php

namespace backend\modules\finance\controllers;



use backend\controllers\AdminController;
use backend\modules\business\models\BusinessPig;
use common\models\business\BusinessContractStatements;
use common\models\business\BusinessCost;
use common\models\business\BusinessCostType;
use common\models\business\BusinessGrowup;
use common\models\business\BusinessPersonCost;
use common\models\business\BusinessSaleorder;
use common\models\shop\ShopHouseOrder;
use common\models\shop\ShopOrder;
use common\models\shop\ShopRaisingOrder;
use yii\db\Query;
use yii\helpers\ArrayHelper;

/**
 * Class SiteController
 * @package backend\modules\finance\controllers
 * 合计统计
 */
class TotalController extends AdminController
{



    /**
     * @return string
     * 应收
     */
    public function actionIndex()
    {
        $sdate = \Yii::$app->request->get('start_date');
        $edate = \Yii::$app->request->get('end_date');
        $sdate = !empty($sdate)?strtotime($sdate):strtotime('-200 days');
        $edate = !empty($edate)?strtotime($edate):time();
//        var_dump(date('Y-m-d',$sdate));
//        var_dump(date('Y-m-d',$edate));

        //取出所有的认养费
        $get['rasprice'] = (new Query())->select('sum(price)')
            ->from(ShopRaisingOrder::tableName())
            ->where('status>=:status and createtime>=:sdate and createtime<=:edate',[':status'=>ShopRaisingOrder::STATUS_PAY,':sdate'=>$sdate,':edate'=>$edate])
            ->scalar();

        //销售金额
        $get['goodsprice'] = (new Query())->select('sum(price)')
            ->from(ShopOrder::tableName())
            ->where('status>=:status and createtime>=:sdate and createtime<=:edate',[':status'=>ShopOrder::STATUS_PAY,':sdate'=>$sdate,':edate'=>$edate])
            ->scalar();

        //农家乐
       $get['houseprice'] = (new Query())->select('sum(cost)')
            ->from(ShopHouseOrder::tableName())
            ->where('status>=:status and createtime>=:sdate and createtime<=:edate',[':status'=>ShopHouseOrder::STATUS_PAY,':sdate'=>$sdate,':edate'=>$edate])
            ->scalar();

        //取出宰杀费
        $get['killprice'] = (new Query())->select('sum(killprice)')
            ->from(BusinessGrowup::tableName())
            ->where('createtime>=:sdate and createtime<=:edate',[':sdate'=>$sdate,':edate'=>$edate])
            ->scalar();

        //取出大客户订购收入
        $get['customerprice'] = (new Query())->select('sum(price)')
            ->from(BusinessSaleorder::tableName())
            ->where('status>=:status and createtime>=:sdate and createtime<=:edate',[':status'=>BusinessSaleorder::STATUS_PAY,':sdate'=>$sdate,':edate'=>$edate])
            ->scalar();
        $get = array_map(function($v){
           return intval($v);
        },$get);
        $get['total'] = array_sum($get);




//        $pay['pig'] = (new Query())->select('sum(cost)')
//            ->from(BusinessPig::tableName())
//            ->where('status>=:status  and intime>=:sdate and intime<=:edate ',[':status'=>BusinessPig::STATUS_AFTERIN,':sdate'=>$sdate,':edate'=>$edate])
//            ->scalar();

        //取计算单的数据为已付的仔猪费用
        $pay['pig'] = (new Query())->select('sum(price)')
            ->from(BusinessContractStatements::tableName())
            ->where('createtime>=:sdate and createtime<=:edate ',[':sdate'=>$sdate,':edate'=>$edate])
            ->scalar();

        $pay['pigcost'] = (new Query())->select('sum(c.price) as sumprice,ct.name,c.typeid')
            ->from(BusinessCost::tableName().' as c')
            ->leftJoin(BusinessCostType::tableName().' as ct','c.typeid=ct.id')
            ->where('status>=:status  and c.createtime>=:sdate and c.createtime<=:edate ',[':status'=>BusinessCost::STATUS_CONFIRM,':sdate'=>$sdate,':edate'=>$edate])
            ->groupBy('typeid')
            ->indexBy('name')
            ->all();

        $pay['personcost'] = (new Query())->select('sum(c.price) as sumprice,c.type,c.typeid')
            ->from(BusinessPersonCost::tableName().' as c')
            ->where('status>=:status  and c.createtime>=:sdate and c.createtime<=:edate ',[':status'=>BusinessPersonCost::STATUS_CONFIRM,':sdate'=>$sdate,':edate'=>$edate])
            ->groupBy('typeid')
            ->indexBy('type')
            ->all();
        $personprice = ArrayHelper::getColumn($pay['personcost'],'sumprice');
        $pigprice = ArrayHelper::getColumn($pay['pigcost'],'sumprice');

        $pay['total'] = array_sum(array_merge($personprice,$pigprice))+$pay['pig'];



        //构造统计图数据
        $tmpnames = array_keys(array_merge($pay['personcost'],$pay['pigcost']));
        $names = array_merge($tmpnames,['认养费','宰杀','线上销售','线下大客户销售','农家乐收入','购买仔猪费']);

        //构造应收
        $gets = [];
        foreach ($get as $key => $row){
            if($row != 0){
                if($key == 'rasprice'){
                    $gets[$key]['name'] = '认养费';
                    $gets[$key]['value'] = $row;
                }else if($key == 'goodsprice'){
                    $gets[$key]['name'] = '线上销售';
                    $gets[$key]['value'] = $row;
                }else if($key == 'houseprice'){
                    $gets[$key]['name'] = '农家乐收入';
                    $gets[$key]['value'] = $row;
                }else if($key == 'killprice'){
                    $gets[$key]['name'] = '宰杀';
                    $gets[$key]['value'] = $row;
                }else if($key == 'customerprice'){
                    $gets[$key]['name'] = '线下大客户销售';
                    $gets[$key]['value'] = $row;
                }
            }

        }
        $gets = array_values($gets);



        //构造应付
        $pays = [];
        foreach ($pay as $key => $row)
        {
            if($key == 'pig'){
                if(!$row != 0){
                    $pays[$key]['name'] = '购买仔猪费';
                    $pays[$key]['value'] = $row;
                }
            }
            if(is_array($row)){
                foreach ($row as $k => $r){
                    if($r['sumprice'] != 0){
                        $pays[$k]['name'] = $k;
                        $pays[$k]['value'] = $r['sumprice'];
                    }
                }
            }
        }
        $pays = array_values($pays);

        return $this->render('index',[
            'get' => $get,
            'pay' => $pay,

            'names' => \GuzzleHttp\json_encode($names),
            'gets' => \GuzzleHttp\json_encode($gets),
            'pays' => \GuzzleHttp\json_encode($pays),
        ]);
    }





}




